package thefirst.controller;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import thefirst.LessonBoard.model.LessonBoardService;
import thefirst.LessonBoard.model.LessonBoardVO;
import thefirst.member.model.MemberVO;
import thefirst.member.model.TeacherVO;
import thefirst.mypage.model.SelectedListService;
import thefirst.mypage.model.SelectedListVO;
import thefirst.mypage.model.TeacherLessonListVO;
@Controller
public class SelectedListController{
	@Resource(name="selectedListServiceImpl") 
	private SelectedListService selectedListService;
	@Resource(name="lessonBoardServiceImpl") 
	private LessonBoardService lessonBoardService;
	
	/**
	 * stList를 보여주는 것을 default로 생각한다.
	 * 로그인한 회원이 학생인지 선생님인지 비교하고 맞는 내용을 출력한다.
	 * @param req
	 * @param mvo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showSelectedList.do")
	public String showSelectedList(HttpServletRequest req, MemberVO mvo) throws Exception {
		System.out.println("Controller > showSelectedListBefore:"+mvo);
		Map m =selectedListService.showSelectedListVO(mvo);
		req.setAttribute("map", m);
		System.out.println("Controller > showSelectedList: "+m);
		String path ="selectedList/selectedList_teacher.mypage";
		if(mvo.getMembertype().equals("s")){
			path="selectedList/selectedList_student.mypage";
		}
		return path;
	}
	
	/**
	 * listtype이 변경되면 
	 * 실행되는 메서드
	 * showSelectedList 와 호출하는 Dao 메서드는 같지만 
	 * listtype을 체크하는 로직이 추가되어 있다.
	 * @param req
	 * @param mvo
	 * @param listtype
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/switchSelectedList.do")
	@ResponseBody
	public Map switchSelectedList(HttpServletRequest req, MemberVO mvo, String listtype) throws Exception {
		//System.out.println("Controller > switchSelectedList:type "+listtype);
		System.out.println("Controller > switchSelectedList:type "+listtype);
		Map m =selectedListService.switchSelectedList(mvo,listtype);
		System.out.println("Controller > switchSelectedList:return "+m);
		return m;
	}
	
	/**
	 * 찜리스트에 등록이 되면 세션에 저장되어 있던 찜목록이 갱신된다.
	 * 등록하고 나서 찜이 되어 있는 정보를 불러온다.
	 * @param session
	 * @param id
	 * @param lessonno
	 * @param listtype
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/registerTLSelectedList.do")
	@ResponseBody
	public String registerTLSelectedList(HttpSession session, String id, int lessonno, String listtype) throws Exception {
		
		System.out.println("Controller > registerSelectedList: "+id+", "+lessonno+", "+listtype);
		selectedListService.registerSelectedList(id, lessonno, listtype);
		List list=selectedListService.getSelectedList(id, listtype);
		session.setAttribute("selectedList", list);
		System.out.println("Controller > registerSelectedList: success");
		return "true";
	}
	
	@RequestMapping("/registerSTSelectedList.do")
	@ResponseBody
	public String registerSTSelectedList(HttpSession session, String studentid, String teacherid, String listtype) throws Exception {
		
		System.out.println("Controller > registerSelectedList: "+studentid+", "+teacherid+", "+listtype);
		selectedListService.registerSelectedList(studentid, teacherid, listtype);
		List list=selectedListService.getSelectedList(studentid, listtype);
		session.setAttribute("selectedList", list);
		System.out.println("Controller > registerSelectedList: success");
		return "true";
	}
	
	/**
	 * 연결상태를 업데이트 하는 메서드이다.
	 * (연결가능 -> 연결완료)
	 * 담임선생님이 누구인지도 기록한다.
	 * @param lbvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/acceptSelectedList.do")
	@ResponseBody
	public LessonBoardVO acceptSelectedList(LessonBoardVO lbvo) throws SQLException{
		System.out.println("===================================");
		System.out.println("연결상태 변경");
		LessonBoardVO rlbvo = lessonBoardService.acceptSelectedList(lbvo);
		System.out.println("연결완료");
		return rlbvo;
	}
	
	/**
	 * 연결상태를 업데이트 하는 메서드이다.
	 * (연결완료 -> 연결가능)
	 * 담임선생님의 값도 null로 세팅
	 * @param lbvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/rejectSelectedList.do")
	@ResponseBody
	public LessonBoardVO rejectSelectedList(LessonBoardVO lbvo) throws SQLException{
		System.out.println("===================================");
		System.out.println("연결상태 변경");
		LessonBoardVO rlbvo = lessonBoardService.rejectSelectedList(lbvo);
		System.out.println("연결완료");
		return rlbvo;
	}
	
	/**
	 * 수업의 연결 상태를 확인하는 메서드
	 * @param lbvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/getConnectstate.do")
	@ResponseBody
	public LessonBoardVO getConnectstate(LessonBoardVO lbvo) throws SQLException{
		System.out.println("===================================");
		System.out.println("연결상태 체크하기");
		LessonBoardVO rlbvo = lessonBoardService.getConnectstate(lbvo);
		System.out.println(rlbvo);
		System.out.println("연결상태 체크완료");
		return rlbvo;
	}
	
	/**
	 * 학생이 찜한 수업 리스트 삭제하는 메서드
	 * @param req
	 * @param id
	 * @param membertype
	 * @param listtype
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteSelectedListVO.do")
	public ModelAndView deleteSelectedListVO(HttpServletRequest req, String id,String membertype, String listtype) throws Exception{
		selectedListService.deleteSelectedListVO(id, listtype);
	
		return new ModelAndView("redirect:/showSelectedList.do?id="+id+"&membertype="+membertype);
	}
}
